package com.medata.utils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.medata.dataObject.Templatefields;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：zb
 * @date ：Created in 2019/5/31 0031 14:52
 * @description：
 */

public class SearchFieldUtils {
    public static List<String> getfield(StringRedisTemplate redisTemplate, String dbid) {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        String tempjson = ops.get(dbid);
        Gson gson=new Gson();
        JsonParser parser=new JsonParser();
        JsonArray jsonArray = parser.parse(tempjson).getAsJsonArray();
        List<String> fields=new ArrayList<>();
        for (JsonElement element:jsonArray){
            Templatefields templatefields = gson.fromJson(element, Templatefields.class);
            if (templatefields.getIsSearchField().equalsIgnoreCase("1")){
                fields.add(templatefields.getTpFieldDesc());
            }
        }
        return fields;
    }
}
